import Vue from 'vue'

import VueRouter from 'vue-router'
import { getToken } from '@/utils/auth'


//注册 组件   router-view    router-link
Vue.use(VueRouter);

let router =  new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'Home',
            component: () => import('@/views/home'),
            children: [
                {
                    path: '/user',
                    name: 'User',
                    component: () => import('@/views/system/user')
                },
                {
                    path: '/role',
                    name: 'Role',
                    component: () => import('@/views/system/role')
                },
                {
                    path: '/menu',
                    name: 'Menu',
                    component: () => import('@/views/system/menu')
                }


            ]
        },
        {
            path: '/login',
            name: 'Login',
            component: () => import('@/views/login')
        },
        {
            path: '*',
            name: '404',
            component: () => import('@/views/404')
        }
    ]
})

let whiteList = ["/login"]
router.beforeEach((to, from, next) => {
    let token = getToken();
    if((token  && token.length>0 ) ||   whiteList.indexOf(to.path) != -1)   {
        next()
    }else{
        next("/login")
    }
   // console.log("from",from)
   // console.log("to",to)
   // console.log("next",next)
})



export default  router;
